<!DOCTYPE html>
<html style="height: 100%">
<head><title>工作台</title>
    <% include layouts/import.ejs %>
</head>
<body style="height: 100%">
<div style="position: relative" class="index height">
    <% include layouts/header.ejs %>
    <div class="index-content container">
        <div class="row">
            <div class="col-md-12 margin-top-15">
                <div class="panel introduce-info">
                    <div class="panel-heading">
                        <ul class="panel-actions">
                            <li><a href="#">更多</a></li>
                        </ul>
                        <h1 class="panel-title">基本介绍</h1></div>
                    <div class="panel-body">
                        <div class="media">
                            <div class="media-body"><h4 class="media-heading">Admui 通用管理系统快速开发框架</h4>
                                <ul style="margin-bottom: 0;" class="list-group list-group-full">
                                    <li class="list-group-item">Admui 是一个基于最新
                                        Web，技术的企业级通用管理系统快速开发框架，可以帮助企业极大的提高工作效率，节省开发成本，提升品牌形象。
                                    </li>
                                    <li class="list-group-item">您可以 Admui 为基础，快速开发各种MIS系统，如CMS、OA、CRM、ERP、POS等。</li>
                                    <li class="list-group-item">Admui 紧贴业务特性，涵盖了大量的常用组件和基础功能，最大程度上帮助企业节省时间成本和费用开支。</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <% include layouts/footer.ejs %>
    <script>

        $(function () {
            $('#bayes-management').addClass('select-active');

            function NB(data) {
                this.fc = {}; //记录特征的数量 feature conut 例如 {a:{yes:5,no:2},b:{yes:1,no:6}}
                this.cc = {}; //记录分类的数量 category conut 例如 {yes:6,no:8}
            }

            NB.prototype={
                infc(w, cls) { //插入新特征值
                    if (!this.fc[w]) this.fc[w] = {};
                    if (!this.fc[w][cls]) this.fc[w][cls] = 0;
                    this.fc[w][cls] += 1;
                },
                incc(cls) { //插入新分类
                    if (!this.cc[cls]) this.cc[cls] = 0;
                    this.cc[cls] += 1;
                },
                allco() { //计算分类总数 all count
                    var t = 0;
                    for (var k in this.cc) t += this.cc[k];
                    return t;
                },
                fprob(w, ct) { //特征标识概率
                    if (Object.keys(this.fc).indexOf(w) >= 0) {
                        if (Object.keys(this.fc[w]).indexOf(ct) < 0) {
                            this.fc[w][ct] = 0
                        }
                        var c = parseFloat(this.fc[w][ct]);//保留两位小数
                        return c / this.cc[ct];
                    } else {
                        return 0.0;
                    }
                },
                cprob(c) { //计算每一个分类概率
                    return parseFloat(this.cc[c] / this.allco());
                },
                train(data, cls) { //参数:学习的Array,标识类型(Yes|No)
                    for (var w of data) this.infc(String(w), cls);
                    this.incc(cls);
                },
                test(data) {
                    var ccp = {}; //P(类别)
                    var fccp = {}; //P(特征|类别)
                    for (var k in this.cc) ccp[k] = this.cprob(k);//计算每一个类别下的概率
                    for (var i of data) {
                        i = String(i);
                        if (!i) continue;
                        if (Object.keys(this.fc).indexOf(i)>= 0) {
                            for (var k in ccp) {
                                if (!fccp[k]) fccp[k] = 1;
                                fccp[k] *= this.fprob(i, k); //P(特征1|类别1)*P(特征2|类别1)*P(特征3|类别1)...
                            }
                        }
                    }
                    var tmpk = "";
                    var tmpkValue = {};
                    for (var k in ccp) {
                        ccp[k] = ccp[k] * fccp[k];
                        tmpkValue[k] = ccp[k]
                        if (!tmpk) tmpk = k;
                        if (ccp[k] > ccp[tmpk]) tmpk = k;
                    }
                    return [tmpk, tmpkValue];

                }
            };



            var nb = new NB();
            var data=[
                {d:["打喷嚏","护士"],c:"感冒"},
                {d:["打喷嚏","农夫"],c:"过敏"},
                {d:["头痛","建筑工人"],c:"脑震荡"},
                {d:["头痛","建筑工人"],c:"感冒"},
                {d:["打喷嚏","教师"],c:"感冒"},
                {d:["头痛","教师"],c:"脑震荡"},
            ];


            for(var i of data){
                nb.train(i.d, i.c);
            }
            console.log(nb)
            console.log(nb.test(["农夫","头痛"]));

        })
    </script>
</div>
</body>
</html>